/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.board;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.account.SysUser;
import com.unis.model.attach.AttachLink;
import com.unis.model.attach.Attachment;
import com.unis.model.board.BoardRoom;
import com.unis.model.board.BoardType;
import com.unis.model.board.Conference;
import com.unis.service.attach.AttachLinkService;
import com.unis.service.attach.AttachmentService;
import com.unis.service.board.BoardRoomService;
import com.unis.service.board.boardtypeService;
import com.unis.service.board.conferenceService;

/**
 * @author Alucard (email:357375084@qq.com)
 * @since 2013-03-14
 */
@Controller
@RequestMapping("/conference")
public class ConferenceController {

    private conferenceService conferenceService;
    private BoardRoomService boardroomService;
    private boardtypeService boardtypeService;
    private AttachLinkService attachlinkService;
    private AttachmentService attachmentService;
    
    /****************************************
     * Insert a instace of conference
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertconference(@RequestBody Conference conference) throws Exception {
        conference.setStatmentId("Conference.insert");
        String status = "failure";
        conferenceService.insert(conference);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of conference
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Conference conference) throws Exception {
        conference.setStatmentId("Conference.update");
        String status = "failure";
        conferenceService.update(conference);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of conference
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Conference conference) throws Exception {
        conference.setStatmentId("Conference.delete");
        String status = "failure";
        conferenceService.batchDelete(conference);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of conference
     ****************************************/
    @RequestMapping("/queryAsList/{check}")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request, @PathVariable String check,ModelMap map) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Conference conference = new Conference();
        //当参数为y时只查询审批人为当前用户的记录
        if("y".equals(check)){
            Subject currentUser = SecurityUtils.getSubject();
            SysUser user = (SysUser)currentUser.getPrincipal();
            conference.setCheckuserid(user.getUserId());
        }
        conference.setStatmentId("Conference.queryAsList");
        BeanUtils.populate(conference, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(conference, pageMap);
        List<BaseModel> conferenceList = conferenceService.queryAsList(conference);
        Map<String, Object> renderMap = GridUIMapper.render(conference, conferenceList);
        return renderMap;
    }

    /****************************************
     * 查询附件
     ****************************************/
    @RequestMapping("/selectAttachment/{conferenceid}")
    @ResponseBody
    public List<?> getAttachment(@PathVariable String conferenceid) throws Exception {
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        AttachLink al = new AttachLink();
        al.setEntityid(conferenceid);
        al.setStatmentId("AttachLink.queryAsList");
        al.setPaging(false);
        List<BaseModel> attachlinklist = attachlinkService.queryAsList(al);
        for (BaseModel basemodel : attachlinklist) {
            AttachLink attachlink = (AttachLink) basemodel;
            Attachment attachment = new Attachment();
            attachment.setFileid(attachlink.getFileid());
            attachment.setStatmentId("Attachment.queryAsObject");
            attachment=(Attachment) attachmentService.queryAsObject(attachment);
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("fileid", attachlink.getFileid());
            map.put("filename", attachment.getFilename());
            list.add(map);
        }
        return list;
    }

    /****************************************
     * 提供会议室选单
     ****************************************/
    @RequestMapping("/selectBoardRoom")
    @ResponseBody
    public List<?> getSelectBoardRoomData(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        if(paramMap.get("starttime")!=null&&paramMap.get("endtime")!=null){
            String starttime=paramMap.get("starttime").toString();
            String endtime=paramMap.get("endtime").toString();
            
            Conference conf=new Conference();
            conf.setStarttime(starttime);
            conf.setEndtime(endtime);
            List<BaseModel> Conferencelist = conferenceService.queryReadyBoardRoom(conf);
            for (BaseModel basemodle : Conferencelist) {
                Conference co = (Conference) basemodle;
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("value", co.getRoomid());
                map.put("label", co.getRoomname());
                list.add(map);
            }
            return list;
        }
        return list;
    }
    
    /****************************************
     * 提供会议议题选单
     ****************************************/
    @RequestMapping("/selectConfTopic")
    @ResponseBody
    public List<?> getSelectConfTopic() throws Exception {
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        Conference conference=new Conference();
        conference.setStatmentId("Conference.queryAsList");
        conference.setPaging(false);
        List<BaseModel> conferences = conferenceService.queryAsList(conference);
        for (BaseModel basemodle : conferences) {
            Conference conf = (Conference) basemodle;
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("value", conf.getConfid());
            map.put("label", conf.getConftopic());
            list.add(map);
        }
        return list;
    }
    /****************************************
     * 提供会议类型选单
     ****************************************/
    @RequestMapping("/selectBoardType")
    @ResponseBody
    public List<?> getSelectBoardTypeData() throws Exception {
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        BoardType boardtype = new BoardType();
        boardtype.setStatmentId("BoardType.queryAsList");
        boardtype.setPaging(false);
        List<BaseModel> boardtypelist = boardtypeService.queryAsList(boardtype);
        for (BaseModel basemodle : boardtypelist) {
            BoardType bt = (BoardType) basemodle;
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("value", bt.getTypeid());
            map.put("label", bt.getTypename());
            list.add(map);
        }
        return list;
    }
    
    /****************************************
     * 根据会议id获取会议内容
     ****************************************/
    @RequestMapping("/selectbyId")
    @ResponseBody
    public String getConfDataById(@RequestBody Conference conference) throws Exception {
        conference.setStatmentId("Conference.queryAsObject");
        conference=(Conference) conferenceService.queryAsObject(conference);
        StringWriter sw = new StringWriter();
        JsonGenerator jsonGenerator = new ObjectMapper().getFactory().createJsonGenerator(sw);
        jsonGenerator.writeObject(conference);
        jsonGenerator.close();
        System.out.println(sw.toString());
        return sw.toString();
    }

    /****************************************
     * Query a instaces of conference
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Conference conference) throws Exception {
        conference.setStatmentId("Conference.queryAsObject");
        return conferenceService.queryAsObject(conference);
    }

    @RequestMapping("/page")
    public String forword() {
        return "board/conferenceList";
    }

    @RequestMapping("/checkpage")
    public String forwordcheck() {
        return "board/conferenceCheckList";
    }

    @Resource
    public void setconferenceService(conferenceService conferenceService) {
        this.conferenceService = conferenceService;
    }

    @Resource
    public void setBoardroomService(BoardRoomService boardroomService) {
        this.boardroomService = boardroomService;
    }

    @Resource
    public void setBoardtypeService(boardtypeService boardtypeService) {
        this.boardtypeService = boardtypeService;
    }

    @Resource
    public void setAttachlinkService(AttachLinkService attachlinkService) {
        this.attachlinkService = attachlinkService;
    }
    
    @Resource
    public void setAttachmentService(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }
    
}
